package fr.utt.lo02.CrazyEights.model.players;

import java.io.Serializable;
import java.util.Random;

import fr.utt.lo02.CrazyEights.model.Game;
import fr.utt.lo02.CrazyEights.model.cards.CardRank;
import fr.utt.lo02.CrazyEights.model.strategies.Strategy;
import fr.utt.lo02.CrazyEights.model.strategies.StrategyBalance;
import fr.utt.lo02.CrazyEights.model.strategies.StrategyRandom;
import fr.utt.lo02.CrazyEights.model.strategies.StrategyScore;

/**
 * Represents a computer player
 * @author Jeremy
 *
 */
public class AI extends Player implements Serializable{

	private static final long serialVersionUID = 1L;
	
	/**
	 * The strategy used by the AI player
	 */
	private Strategy strategy;
	
	/**
	 * Create the AI player and set a Strategy
	 * @param name name wanted for the player
	 */
	public AI(String name){
		super(name);
		setStrategy();
	}
	
	/**
	 * Play using the defined strategy
	 */
	public void play() {
		play(strategy.play(this));
	}
	
	/**
	 * Choose suit using the defined strategy
	 */
	public void chooseSuit(){
		Game.getInstance().getDiscardPile().setGameSuit(strategy.chooseSuit(this));
		if(Game.getInstance().getDiscardPile().getCardOnTop().getRank()==CardRank.Joker){
			Game.getInstance().getPlayersList().skipNextPlayer();
		}
		else{
			Game.getInstance().getPlayersList().nextPlayer();
		}
	}
	
	/**
	 * Set strategy randomly
	 */
	public void setStrategy(){
		Random rand = new Random();
		int min = 0;
		int max = 2;
		int randomStrat = rand.nextInt(max-min+1)+min;
		switch(randomStrat){
		case 0:
			setStrategy(new StrategyRandom());
			break;
		case 1:
			setStrategy(new StrategyBalance());
			break;
		case 2:
			setStrategy(new StrategyScore());
			break;
		default:
			setStrategy(new StrategyRandom());
		}
	}
	
	/**
	 * Set the strategy of the player
	 * @param strategy strategy wanted
	 */
	public void setStrategy(Strategy strategy){
		this.strategy = strategy;
	}
	
	public String toString() {
		return "[B] "+getName();
	}
}
